'use strict';

let nunjucks = require('nunjucks');

function createEnv(path, opts) {
    path = path || 'view';
    opts = opts || {};
    let nunjucksConfig = {
        autoescape :opts.autoescape || true,
        throwOnUndefined :opts.throwOnUndefined || false,
        trimBlocks: opts.trimBlocks || true,
        lstripBlocks :opts.lstripBlocks || false,
        watch: opts.watch || true,
        noCaChe: opts.noCaChe || true,
    }
    let env = nunjucks.configure('view', nunjucksConfig);
    return env;
}

let middleWare = async (ctx, next) => {
    let env = createEnv();
    ctx.render = (view, model) => {
        ctx.body = env.render(view, model);
    }
    await next();
}

module.exports = middleWare;